package com.winkey.facility.gateway.service.face.impl;


import com.alibaba.fastjson.JSONObject;
import com.winkey.facility.gateway.connector.gate.face.oem.baisheng.BaiShengFaceGate;
import com.winkey.facility.gateway.service.face.FaceFacilityService;
import com.winkey.facility.gateway.service.utils.Constants;
import com.winkey.facility.gateway.service.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author Administrator
 */
@Service
public class BaiShangFaceServiceImpl implements FaceFacilityService {
    @Autowired
    private BaiShengFaceGate baiShengFaceGate;
    @Override
    public String save(String deviceNo, String name, String tag) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId",Constants.BS_APP_ID);
        jsonObject.put("deviceNo",deviceNo);
        jsonObject.put("deviceName",name);
        jsonObject.put("tag",tag);
        try {
            String result = HttpUtils.sendBSPost(Constants.BS_PREFIX+"/device/bs/face/save",jsonObject.toJSONString(),"application/json",baiShengFaceGate.auth(null));
            jsonObject = JSONObject.parseObject(result);
            return jsonObject.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String modify() {
        return null;
    }

    @Override
    public String page(int size) {
        try {
            return  HttpUtils.sendBSGet(Constants.BS_PREFIX+"/device/bs/face/page","appId="+Constants.BS_APP_ID+"&size="+size,"application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String single(String deviceNo) {
        try {
            return  HttpUtils.sendBSGet(Constants.BS_PREFIX+"/device/bs/face/findConfigByDeviceNo/"+Constants.BS_APP_ID+"/"+deviceNo,"","application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String disable(String deviceNo){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId",Constants.BS_APP_ID);
        jsonObject.put("deviceNo",deviceNo);
        String result = null;
        try {
            result = HttpUtils.sendBSPost(Constants.BS_PREFIX+"/device/bs/face/disable",jsonObject.toJSONString(),"application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject = JSONObject.parseObject(result);
        return jsonObject.toJSONString();
    }
    @Override
    public String enable(String deviceNo){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId",Constants.BS_APP_ID);
        jsonObject.put("deviceNo",deviceNo);
        String result = null;
        try {
            result = HttpUtils.sendBSPost(Constants.BS_PREFIX+"/device/bs/face/enable",jsonObject.toJSONString(),"application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject = JSONObject.parseObject(result);
        return jsonObject.toJSONString();
    }

    @Override
    public String remove(String deviceNo){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId",Constants.BS_APP_ID);
        jsonObject.put("deviceNo",deviceNo);
        String result = null;
        try {
            result = HttpUtils.sendBSPost(Constants.BS_PREFIX+"/device/bs/face/remove",jsonObject.toJSONString(),"application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject = JSONObject.parseObject(result);
        return jsonObject.toJSONString();
    }

    @Override
    public String remoteOpenDoor(String deviceNo){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId",Constants.BS_APP_ID);
        jsonObject.put("deviceNo",deviceNo);
        String result = null;
        try {
            result = HttpUtils.sendBSPost(Constants.BS_PREFIX+"/device/bs/face/remoteOpenDoor",jsonObject.toJSONString(),"application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject = JSONObject.parseObject(result);
        return jsonObject.toJSONString();
    }

    @Override
    public String setOpenDoorCondition(String deviceNo,String openDoorWay) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appId",Constants.BS_APP_ID);
        jsonObject.put("deviceNo",deviceNo);
        jsonObject.put("openDoorWay",openDoorWay);
        String result = null;
        try {
            result = HttpUtils.sendBSPost(Constants.BS_PREFIX+"/device/bs/face/setOpenDoorCondition",jsonObject.toJSONString(),"application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        jsonObject = JSONObject.parseObject(result);
        return jsonObject.toJSONString();
    }

    @Override
    public String findDeviceOnlineStatus(String deviceNo){
        long timeMill = System.currentTimeMillis();
        String sign = baiShengFaceGate.encryption(Constants.BS_APP_KEY+timeMill+Constants.BS_APP_SECRET);
        try {
            return HttpUtils.sendBSGet(Constants.BS_PREFIX+"/device/bs/face/findDeviceOnlineStatus/"+Constants.BS_APP_ID,"deviceNos="+deviceNo,"application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String refeshStatus(String deviceNos) {
        try {
            return  HttpUtils.sendBSGet(Constants.BS_PREFIX+"/device/bs/face/findDeviceOnlineStatus/"+Constants.BS_APP_ID,"deviceNos="+deviceNos,"application/json",baiShengFaceGate.auth(null));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String authPersionPage() {
        return null;
    }

    @Override
    public String singlePersonAuth() {
        return null;
    }

    @Override
    public String singlePersonRevoke() {
        return null;
    }

    @Override
    public String batchAuth() {
        return null;
    }

    @Override
    public String modifyAuth() {
        return null;
    }
}
